package com.yykj.tencent.im.server.model.repository;

import com.yykj.tencent.im.server.dto.FriendDTO;
import com.yykj.tencent.im.server.dto.GroupDTO;
import com.yykj.tencent.im.server.model.entity.Friend;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 好友接口类
 */
@Repository
public interface FriendRepository extends JpaRepository<Friend,Integer>, JpaSpecificationExecutor<Friend> {

    Friend findByFromAccountAndToAccount(String fromAccount,String toAccount);


    /**
     * 我的好友
     * @param env
     * @param fromAccount
     * @return
     */
    @Query("select f from Friend f where f.environment = :env and f.fromAccount = :fromAccount and f.type = :type")
    List<Friend> myFriends(@Param("env") String env,@Param("fromAccount") String fromAccount,@Param("type") String type);



    @Query("select f from Friend f where f.environment = :env and f.fromAccount = :fromAccount")
    Page<Friend> queryFriend(@Param("env") String env,@Param("fromAccount") String fromAccount, Pageable page);


    /**
     * 根据账号查询出常用联系好友
     * @param accounts 账号列表
     * @return
     */
    @Query("select new com.yykj.tencent.im.server.dto.FriendDTO(" +
            "u.fromAccount,u.name,u.image )" +
            " from UserProfile as u " +
            " where u.fromAccount in (:accounts)")
    List<FriendDTO> queryFriendByAccount(@Param("accounts") List<String> accounts);

    /**
     * 根据账号查询出常用联系团队
     * @param groupIds 账号列表
     * @return
     */
    @Query("select new com.yykj.tencent.im.server.dto.GroupDTO(" +
            "g.groupId,g.name,g.faceUrl)" +
            " from GroupProfile as g " +
            " where g.groupId in (:groupIds)")
    List<GroupDTO> queryGroupById(@Param("groupIds") List<String> groupIds);

    /**
     * 删除好友
     */
    @Query("delete from Friend f where f.environment = :env and f.fromAccount = :fromAccount" +
            " and f.toAccount = :toAccount ")
    void deleteMyFriend(@Param("env") String env
            ,@Param("fromAccount") String fromAccount
            ,@Param("toAccount") String toAccount);

}
